package dao

import (
	"context"
	"exchange/internal/model"
	"gorm.io/gorm"
	"mscoin-common/msdb"
	"mscoin-common/msdb/gorms"
)

// mysql操作
type ExchangeOrderDao struct {
	conn *gorms.GormConn
}

// 查看历史订单
func (e *ExchangeOrderDao) FindOrderHistory(ctx context.Context, symbol string, page int64, size int64, memberId int64) (list []*model.ExchangeOrder, total int64, err error) {
	session := e.conn.Session(ctx)
	err = session.Model(&model.ExchangeOrder{}).
		Where("symbol=? and member_id=?", symbol, memberId).
		Limit(int(size)).
		Offset(int((page - 1) * size)).Find(&list).Error
	err = session.Model(&model.ExchangeOrder{}).
		Where("symbol=? and member_id=?", symbol, memberId).
		Count(&total).Error
	return
}

// 查看当前交易订单
func (e *ExchangeOrderDao) FindOrderCurrent(ctx context.Context, symbol string, page int64, size int64, memberId int64) (list []*model.ExchangeOrder, total int64, err error) {
	session := e.conn.Session(ctx)
	err = session.Model(&model.ExchangeOrder{}).
		Where("symbol=? and member_id=? and status=?", symbol, memberId, model.Trading).
		Limit(int(size)).
		Offset(int((page - 1) * size)).Find(&list).Error
	err = session.Model(&model.ExchangeOrder{}).
		Where("symbol=? and member_id=? and status=?", symbol, memberId, model.Trading).
		Count(&total).Error
	return
}

func (e *ExchangeOrderDao) FindCurrentTradingCount(ctx context.Context, id int64, symbol string, direction int) (int64, error) {
	session := e.conn.Session(ctx)
	var total int64
	err := session.Model(&model.ExchangeOrder{}).Where("symbol=? and member_id = ? and direction = ? and status = ?", symbol, id, direction, model.Trading).Count(&total).Error
	if err != nil {
		return 0, nil
	}
	return total, nil
}

func (e *ExchangeOrderDao) Save(ctx context.Context, conn msdb.DbConn, order *model.ExchangeOrder) error {
	e.conn = conn.(*gorms.GormConn)
	tx := e.conn.Tx(ctx)
	err := tx.Save(&order).Error
	return err
}

// 根据订单id查询交易订单
// select * from exchange_order where order_id = order_id
func (e *ExchangeOrderDao) FindOrderByOrderId(ctx context.Context, orderId string) (*model.ExchangeOrder, error) {
	order := &model.ExchangeOrder{}
	session := e.conn.Session(ctx)
	err := session.Model(&model.ExchangeOrder{}).Where("order_id=?", orderId).Take(&order).Error
	if err != nil && err == gorm.ErrRecordNotFound {
		return nil, err
	}
	return order, nil
}

// 修改订单的状态为取消状态
func (e ExchangeOrderDao) UpdateStatusCancel(ctx context.Context, orderId string) error {
	session := e.conn.Session(ctx)
	err := session.Model(&model.ExchangeOrder{}).Where("order_id=?", orderId).Update("status", model.Canceled).Error
	return err
}

// 修改订单的状态为正在交易的状态0
func (e ExchangeOrderDao) UpdateOrderStatusTrading(ctx context.Context, orderId string) error {
	session := e.conn.Session(ctx)
	err := session.Model(&model.ExchangeOrder{}).Where("order_id=?", orderId).Update("status", model.Trading).Error
	return err
}

func NewExchangeOrderDao(db *msdb.MsDB) *ExchangeOrderDao {
	return &ExchangeOrderDao{
		conn: gorms.New(db.Conn),
	}
}
